package com.citycloud.ccuap.tc.sync.aspect;


import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.sys.entity.SysProject;
import com.citycloud.ccuap.commons.sys.entity.SysUser;
import com.citycloud.ccuap.commons.sys.project.IProjectService;
import com.citycloud.ccuap.commons.sys.user.UserService;
import com.citycloud.ccuap.tc.sync.dto.UserMessageDTO;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncOperation;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncType;
import com.citycloud.ccuap.tc.sync.dto.SyncDataDto;
import com.citycloud.ccuap.tc.sync.service.IMessageService;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author zjl
 * @date 2020/04/30 9:58
 */
@Aspect
@Component
public class UserImportAspect {
    @Autowired
    private UserService userService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private IProjectService projectService;

    @Pointcut("execution(* com.citycloud.ccuap.commons.sys.user.UserController.importUser())")
    public void importUser() {
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @AfterReturning(value = "importUser()", returning = "key")
    public void doAddUser(JoinPoint joinPoint, Object key) throws Throwable {
        if (key instanceof JSONData) {
            JSONData jsonData = (JSONData) key;
            ImmutableMap<String, Object> data = (ImmutableMap) jsonData.getData();
            Collection<Long> addUserId = (Collection<Long>) data.get("adds");
            Long deptId = (Long) data.get("deptId");
            Long projectId = (Long) data.get("projectId");

            if (CollectionUtils.isNotEmpty(addUserId)) {
                List<SysUser> users = userService.selectBatchIds(new ArrayList<>(addUserId));
                for (SysUser user : users) {
                    user.setDeptId(deptId);
                }
                //MQ 推送站点新增用户
                sendImportMessage(users, projectId);
            }
        }
    }

    private void sendImportMessage(List<SysUser> users, Long projectId) {
        if (projectId == null || CollectionUtils.isEmpty(users)) {
            return;
        }
        SysProject project = projectService.findById(projectId);
        if (project != null && project.getMqOff()) {
            List<UserMessageDTO> list = new ArrayList<>();
            users.forEach(user -> {
                UserMessageDTO userMessageDTO = new UserMessageDTO();
                userMessageDTO.build(user);
                userMessageDTO.setProjectId(projectId);
                list.add(userMessageDTO);
            });
            sendMessage(projectId, list);
        }
    }

    private void sendMessage(Long projectId, List<UserMessageDTO> list) {
        SyncDataDto<List<UserMessageDTO>> result = SyncDataDto.of(GlobalSyncType.OBJECT_TYPE_USER,
                GlobalSyncOperation.CREATED, projectId, list);
        messageService.sendMessageByTopic(result,GlobalSyncType.TOPIC_USER_DEPT);
    }

}
